package com.huxin.servelt.admin;

import com.alibaba.fastjson.JSON;
import com.huxin.pojo.AdminInfo;
import com.huxin.pojo.BookTypeInfo;
import com.huxin.service.Userservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 管理员登录
 */
@WebServlet("/admin/login")
public class AdminLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String s = request.getReader().readLine();
        AdminInfo adminInfo = JSON.parseObject(s, AdminInfo.class);
        Userservice userservice = new Userservice();
        AdminInfo adminLogin=userservice.adminLogin(adminInfo);
        if (adminLogin!=null){
            Cookie username = new Cookie("admin",adminLogin.getOperatorName());
            Cookie password = new Cookie("password",adminLogin.getOperatorPass());

            username.setMaxAge(3600);
            password.setMaxAge(3600);
            //响应中增加cookie
            response.addCookie(username);
            response.addCookie(password);
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
